/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.poll.alarm.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.poll.alarm.entity.PollAlarmEntity;
import com.hopes.bizz.poll.alarm.entity.PollAlarmExceptionEntity;
import com.hopes.bizz.poll.alarm.entity.PollAlarmNetEntity;
import com.hopes.bizz.poll.alarm.service.PollAlarmExceptionService;
import com.hopes.bizz.poll.alarm.service.PollAlarmNetService;
import com.hopes.bizz.poll.alarm.service.PollAlarmService;
import com.hopes.bizz.poll.common.constant.AlarmConstant;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.log.annotation.SysLog;
import com.hopes.bizz.poll.alarm.entity.PollAlarmHandleRecordEntity;
import com.hopes.bizz.poll.alarm.service.PollAlarmHandleRecordService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 报警处理记录
 *
 * @author daijie
 * @date 2023-05-31 10:46:16
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/alarm/record")
@Tag(description = "alarm/record", name = "报警处理记录管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class PollAlarmHandleRecordController {

    private final PollAlarmHandleRecordService pollAlarmHandleRecordService;

    private final PollAlarmNetService pollAlarmNetService;

    private final PollAlarmService pollAlarmService;

    private final PollAlarmExceptionService pollAlarmExceptionService;


    /**
     * 分页查询
     *
     * @param page                  分页对象
     * @param pollAlarmHandleRecord 报警处理记录
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('alarm_record_view')")
    public R getpollAlarmHandleRecordPage(Page page, PollAlarmHandleRecordEntity pollAlarmHandleRecord) {
        LambdaQueryWrapper<PollAlarmHandleRecordEntity> wrapper = Wrappers.lambdaQuery();
        return R.ok(pollAlarmHandleRecordService.page(page, wrapper));
    }


    /**
     * 通过id查询报警处理记录
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('alarm_record_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(pollAlarmHandleRecordService.getById(id));
    }

    /**
     * 新增报警处理记录
     *
     * @param pollAlarmHandleRecord 报警处理记录
     * @return R
     */
    @Operation(summary = "新增报警处理记录", description = "新增报警处理记录")
    @SysLog("新增报警处理记录")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('alarm_record_add')")
    @Transactional(rollbackFor = Exception.class)
    public R save(@RequestBody PollAlarmHandleRecordEntity pollAlarmHandleRecord) {
        String moduleCode = pollAlarmHandleRecord.getModuleCode();
        Long alarmId = pollAlarmHandleRecord.getAlarmId();
        if (AlarmConstant.ALARM_TYPE_ALARM.equals(moduleCode)) {
            PollAlarmEntity entity = pollAlarmService.getById(alarmId);
            entity.setHandleStatus(AlarmConstant.ALARM_HANDLE_STATUS_ALREADY);
            pollAlarmService.updateById(entity);
            pollAlarmHandleRecord.setAlarmTime(entity.getAlarmTime());
            pollAlarmHandleRecord.setAlarmContent(entity.getAlarmContent());
        } else if (AlarmConstant.ALARM_TYPE_NET.equals(moduleCode)) {
            PollAlarmNetEntity entity = pollAlarmNetService.getById(alarmId);
            entity.setHandleStatus(AlarmConstant.ALARM_HANDLE_STATUS_ALREADY);
            pollAlarmNetService.updateById(entity);
            pollAlarmHandleRecord.setAlarmTime(entity.getAlarmTime());
            pollAlarmHandleRecord.setAlarmContent(entity.getAlarmContent());
        } else if (AlarmConstant.ALARM_TYPE_EXCEPTION.equals(moduleCode)) {
            PollAlarmExceptionEntity entity = pollAlarmExceptionService.getById(alarmId);
            entity.setHandleStatus(AlarmConstant.ALARM_HANDLE_STATUS_ALREADY);
            pollAlarmExceptionService.updateById(entity);
            pollAlarmHandleRecord.setAlarmTime(entity.getAlarmTime());
            pollAlarmHandleRecord.setAlarmContent(entity.getAlarmContent());
        }
        return R.ok(pollAlarmHandleRecordService.save(pollAlarmHandleRecord));
    }

    /**
     * 修改报警处理记录
     *
     * @param pollAlarmHandleRecord 报警处理记录
     * @return R
     */
    @Operation(summary = "修改报警处理记录", description = "修改报警处理记录")
    @SysLog("修改报警处理记录")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('alarm_record_edit')")
    public R updateById(@RequestBody PollAlarmHandleRecordEntity pollAlarmHandleRecord) {
        return R.ok(pollAlarmHandleRecordService.updateById(pollAlarmHandleRecord));
    }

    /**
     * 通过id删除报警处理记录
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除报警处理记录", description = "通过id删除报警处理记录")
    @SysLog("通过id删除报警处理记录")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('alarm_record_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(pollAlarmHandleRecordService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     *
     * @param pollAlarmHandleRecord 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('alarm_record_export')")
    public List<PollAlarmHandleRecordEntity> export(PollAlarmHandleRecordEntity pollAlarmHandleRecord) {
        return pollAlarmHandleRecordService.list(Wrappers.query(pollAlarmHandleRecord));
    }
}